﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kooboo.IoC;
using Kooboo.Domain;

namespace Kooboo.Commerce
{
    public static class SupportTicketCommand
    {
        public static void Close(this ISupportTicket support)
        {
            var list = new List<ISupportTicket>(){ support};
            list.Close();
        }

        public static void GivenUp(this ISupportTicket support)
        {
            var list = new List<ISupportTicket>() { support };
            list.GivenUp();
        }

        public static void Close(this IEnumerable<ISupportTicket> supports)
        {
            
            using (var transaction = ObjectContainer.CreateInstance<TransactionContext>())
            {
                try
                {     
                    supports
                        .ToUpdate()
                        .Set(i => i.Status,SupportStatus.Closed)
                        .Execute();
                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }

        public static void GivenUp(this IEnumerable<ISupportTicket> supports)
        {
            
            using (var transaction = ObjectContainer.CreateInstance<TransactionContext>())
            {
                try
                {
                    supports
                        .ToUpdate()
                        .Set(i => i.Status, SupportStatus.GivenUp)
                        .Execute();
                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }

        public static void InProgress(this IEnumerable<ISupportTicket> supports)
        {
            

            using (var transaction = ObjectContainer.CreateInstance<TransactionContext>())
            {
                try
                {
                    supports
                        .ToUpdate()
                        .Set(i => i.Status, SupportStatus.InProgress)
                        .Execute();

                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }
        
    }
}
